home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / QuickDraw™ GX / Programming Stuff / Sample Code / Graphics Samples / rotating rects ƒ / Rotating Rects.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  5.3 KB  |  168 lines  |  [TEXT/KAHL]

  1. /*
  2.     Rotating Rects
  3.     
  4.     This application will draw rectangles within the bounds the window.  The rectangles will be rotated, scaled, and 
  5.     colored with an HSV color.  All of the functions in this file are called by the graphics shell.
  6.     
  7.     NOTES:
  8.     • This file requires the following files to run correctly:
  9.         "graphics shell.c"  and "graphics debug library.c"
  10.         
  11.     • For the best printing results, print this file in "landscape".
  12.     
  13.     ©1990 - 1994  Apple Computer, Inc.
  14.     All rights reserved.
  15. */
  16.  
  17.  
  18. #include <Events.h>
  19. #include <Windows.h>
  20.  
  21. #include "graphics libraries.h"
  22. #include "graphics toolbox.h"
  23. #include "graphics shell.h"
  24.  
  25. #define f(a,b)    (((fixed) (a) << 16) + (b))
  26. #define kNumberOfRectanglesDrawn     110
  27.  
  28. //
  29. //  Set up the title and size of the window 
  30. //
  31. Str255         gWindowTitle = "\p Rotating Rects (dither level = 4)";
  32. Rect         gWindowQDRect  = {50, 50, 330, 330};
  33.  
  34. //
  35. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  36. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  37. //    With gGraphicsHeapSize set to 25k, I had 3 free blocks left in the graphics gxHeap. Sounds good to me.
  38. //
  39. long            gGraphicsHeapSize = 25;
  40.  
  41. gxShape         gRectangleShape;
  42. gxRectangle     gFixedWindowBounds;           /**  bounding box of the window in local coordinates  **/
  43. gxColor         gRectangleColor;
  44. Boolean            gChangeShapeFill;
  45. short            gTotalnumberOfRectanglesDrawn;
  46.  
  47.  
  48. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  49.  
  50. void DoInitialization(aWindow)
  51. WindowPtr aWindow;
  52. {
  53.     gxViewPort        windowViewPortParent;
  54.     
  55.     gChangeShapeFill = true;
  56.     gTotalnumberOfRectanglesDrawn = 0;
  57.      
  58.     //
  59.     //    Get the viewPort that is attached to the window, and set the dither of this viewPort to 4.  Which
  60.     //    will mean that all objects that are drawn into window will be dithered at a dither level of 4.
  61.     //
  62.      windowViewPortParent = GXGetWindowViewPort(aWindow);
  63.     GXSetViewPortDither(windowViewPortParent, 4);
  64.  
  65.     //
  66.     //     Get the bounds of the window, and create a gxRectangle that will fill the window
  67.     //
  68.     GXGetShapeBounds(gWindowBoundsShape, 0L, &gFixedWindowBounds);
  69.     gRectangleShape = GXNewRectangle(&gFixedWindowBounds); 
  70.     
  71.     //
  72.      //     Set up an HSV gxColor space to run the rectangles through...  
  73.     //
  74.     gRectangleColor.space = gxHSVSpace;
  75.     gRectangleColor.profile = nil;
  76.     gRectangleColor.element.hsv.hue = 0x0000;
  77.     gRectangleColor.element.hsv.value = 0xFFFF;
  78.     gRectangleColor.element.hsv.saturation = 0xFFFF;
  79. }
  80.  
  81.  
  82. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  83.  
  84. void DoDraw(aWindow)
  85. WindowPtr aWindow;
  86. {
  87.     gxRectangle     rectangleBoundsShape;
  88.     fixed             x, y;
  89.  
  90.     if (gTotalnumberOfRectanglesDrawn ==  kNumberOfRectanglesDrawn) {
  91.         
  92.         //
  93.          //  Time to rebuild the gxRectangle...   Dispose of the "old" rectangle, and re-create a rectangle that will fill the window 
  94.         //
  95.         GXDisposeShape(gRectangleShape); 
  96.         gRectangleShape = GXNewRectangle(&gFixedWindowBounds); 
  97.     
  98.         //
  99.         //   Set the gxShapeFill type, alternate drawing the gxRectangle with a solid fill and a  gxClosedFrameFill
  100.         //
  101.         if (gChangeShapeFill == true) {
  102.              SetPort (aWindow);
  103.             EraseRect(&(aWindow)->portRect);
  104.             GXSetShapeFill (gRectangleShape, gxClosedFrameFill);
  105.                    gChangeShapeFill = false;
  106.             } 
  107.            else  gChangeShapeFill = true;
  108.                   
  109.         gTotalnumberOfRectanglesDrawn = 0;
  110.     }
  111.     
  112.     GXSetShapeColor(gRectangleShape, &gRectangleColor);
  113.     GXDrawShape(gRectangleShape);
  114.  
  115.     gRectangleColor.element.hsv.hue += 0x0300;
  116.     
  117.     //
  118.     //    Get the new bounds of the rectangle after it has been scaled by calling GXScaleShape. Each call to GXScaleShape
  119.     //    changes the bounding shape (box) of the rectangle. Determine the center of the bounding box, pass the center
  120.     //    to the GXRotateShape call. This will have the rectagnel rotated about the center.
  121.     //
  122.     GXGetShapeBounds(gRectangleShape, 0L, &rectangleBoundsShape);
  123.     x = rectangleBoundsShape.left + rectangleBoundsShape.right >> 1;
  124.     y = rectangleBoundsShape.top + rectangleBoundsShape.bottom >> 1;
  125.             
  126.     GXRotateShape(gRectangleShape, ff(2), x, y);
  127.     GXScaleShape(gRectangleShape, f(0, 0xF800), f(0, 0xF800), x, y);
  128.  
  129.     gTotalnumberOfRectanglesDrawn++;
  130. }
  131.  
  132.  
  133. /*------ DoDispose -------------------------------------------------------------------------------------*/
  134.  
  135. void DoDispose(aWindow)
  136. WindowPtr aWindow;
  137. {
  138.      //  
  139.     //    You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  140.     //    form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  141.     //    call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  142.     //    SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  143.     //    can turn notices on in this file by setting gDebugging = TRUE (above).
  144.     //  
  145.       GXDisposeShape(gRectangleShape); 
  146.     GXDisposeShape(gWindowBoundsShape);  
  147.     DisposeWindow(aWindow);
  148. }
  149.     
  150.  
  151.  
  152. /*------ DoClick ---------------------------------------------------------------------------------------*/
  153.  
  154. void DoClick( orgMouseLoc, theWindow )
  155. gxPoint        orgMouseLoc;
  156. WindowPtr     theWindow;
  157. {
  158. }
  159.  
  160.  
  161. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  162.  
  163. void DoIdle(aWindow)
  164. WindowPtr aWindow;
  165. {
  166.     DoDraw(aWindow);
  167. }
  168.